﻿using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_AddManager : Form
    {
        #region 属性
        public byte[] PhotoByte = null;

        public Guid ManagerID { get; set; }

        public event EventHandler SaveEvent;

        public enum EStatus
        {
            Add = 0,
            Modify = 1,
        }

        public EStatus FStatus;
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddManager(EStatus status)
        {
            InitializeComponent();
            this.FStatus = status;
        }
        public Frm_AddManager(Guid managerID, EStatus status)
        {
            InitializeComponent();
            this.ManagerID = managerID;
            this.FStatus = status;
        }
        #endregion

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddManager_Load(object sender, EventArgs e)
        {
            if (FStatus == EStatus.Add)
            {
                this.Text = "管理员 - 添加";
            }
            if (FStatus == EStatus.Modify)
            {
                AppService aservice = new AppService();
                Manager man = aservice.GetManagerByID(ManagerID);
                NameEdit.Text = man.Name;
                PasswordEdit.Text = man.Password;
                PhotoButtonEdit.Text = "<已修改>";
                this.Text = "管理员 - 编辑";
            }
        }

        /// <summary>
        /// 相片按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PhotoButtonEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "打开文件";
            open.ShowHelp = true;
            open.Filter = "图片文件（*.jpg;*.gif;*.bmp）|*.jpg;*.gif;*.bmp";
            open.FilterIndex = 1;
            open.RestoreDirectory = false;
            open.InitialDirectory = "C:\\";
            open.Multiselect = false;
            if (open.ShowDialog() == DialogResult.OK)
            {
                PhotoButtonEdit.Text = open.FileName;
                //将字符串转换成字节
                PhotoByte = File.ReadAllBytes(open.FileName);
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            Manager manager = null;

            if (FStatus == EStatus.Add)
            {
                if (string.IsNullOrEmpty(NameEdit.Text) || string.IsNullOrEmpty(PasswordEdit.Text) || string.IsNullOrEmpty(PhotoButtonEdit.Text))
                {
                    MessageBox.Show("请完善信息!");
                }
                else
                {
                    manager = aservice.AddManager(NameEdit.Text, PasswordEdit.Text, PhotoByte);
                    this.Hide();
                    MessageBox.Show("添加成功!");
                }
            }
            else if (FStatus == EStatus.Modify)
            {
                if (string.IsNullOrEmpty(NameEdit.Text) || string.IsNullOrEmpty(PasswordEdit.Text) || string.IsNullOrEmpty(PhotoButtonEdit.Text))
                {
                    MessageBox.Show("请完善信息!");
                }
                else
                {
                    manager = aservice.UpdateManager(ManagerID,NameEdit.Text,PasswordEdit.Text,PhotoByte);
                    this.Hide();
                    MessageBox.Show("编辑成功!");
                }
            }

            if (SaveEvent != null)
            {
                SaveEvent(manager, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
